// pages/shopingmall/index.js
const app = getApp() 
const _request = require("../../utils/request.js"); 

Page({

  /**
   * 页面的初始数据
   */
  data: {
    mallimgUrl: app.globalData.mallimgUrl,
    banners: [],
    indicatorDots: true,
    vertical: false,
    autoplay: true,
    interval: 2000,
    duration: 500,
    toolbarList: [],
    subject1: {},
    subject2: {},
    subject3: {},
    active: 0,
    goodslist: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.loadAdvertisingList()
    this.loadGoodsCategory()
    this.loadSubject()
    this.loadGoodsList()
  },
  goyidou(){ 
    wx.showToast({
      title: '功能建设中,尽情期待！',
      icon:'none'
    })
    return;
    wx.navigateTo({
      url: '/pagesb/index/index',
    })
  },
  gotoNext: function(item) {
    wx.navigateTo({
      url: '/pages/shopingmall/goodsdetail' + this.getparams(item.currentTarget.dataset)
    })
  },

  gotoShopcar() {
    wx.navigateTo({
      url: '/pages/shopingmall/shopcar'
    })
  },

  gotoItemIndex(e) {
    const {
      item
    } = e.currentTarget.dataset
    if (item.name == '意豆专区') {
      this.goyidou()
    } else {
      wx.navigateTo({
        url: '/pages/shopingmall/itemindex' + this.getparams(e.currentTarget.dataset)
      })
    }
  },

  gotoSearch: function() {
    wx.navigateTo({
      url: '/pages/shopingmall/searchindex'
    })
  },

  getparams: function(item) {
    const list = []
    for(const key in item) {
      if (key === 'url') {
        continue;
      }
      list.push(key + '=' + item[key])
    }

    if (list.length) {
      return '?' + list.join('&')
    } else {
      return '';
    }

  },

  /**
   * 加载广告列表
   */
  loadAdvertisingList: function() {
    const self = this
    _request.postObj('/marketing/advertising/getlist', {
      type: 1
    }, app.globalData.mallType).then((res) => {
      // console.log(res)
      if (res.data.code === 200) {
        self.setData({
          banners: res.data.data.rows || []
        })
      }
    })
  },
  /**
   * 加载商品分类
   */
  loadGoodsCategory: function() {
    const self = this
    _request.get('/commodity/getcommoditylist', {}, app.globalData.mallType).then((res) => {
      // console.log(res)
      if (res.data.code === 200) {
        let obj={
          icon: "https://kjyiyue.oss-cn-shenzhen.aliyuncs.com/images/yidoushop.png",
          name: "意豆专区",
          fun: 'goyidou',
        }
        self.setData({
          toolbarList: [...res.data.data,obj] || []
        })
      }
    })
  },

  /**
   * 加载专题列表
   */
  loadSubject: function() {
    const self = this
    _request.postObj('/marketing/special/getlist', {}, app.globalData.mallType).then((res) => {
      // console.log(res)
      if (res.data.code === 200) {
        const list = res.data.data.rows || []
        self.setData({
          subject1: list[0],
          subject2: list[1],
          subject3: list[2]
        })
      }
    })
  },
  
  loadGoodsList: function() {
    const self = this
    this.setData({
      goodslist: []
    })
    _request.get('/commodity/getConditionStatusProductList', {status: this.data.active}, app.globalData.mallType).then((res) => {
      // console.log(res)
      if (res.data.code === 200) {
        let list = res.data.data.rows || []
        list.forEach(e => {
          e.originalPrice = (e.originalPrice || 0).toFixed(2)
          e.price = (e.price || 0).toFixed(2)
        })
        self.setData({
          goodslist:  list
        })
      }
    })
  },
  queryListByType(e){
    this.setData({
      active: e.currentTarget.dataset.index
    })
    this.loadGoodsList()
  },
})